const mysql = require('mysql2');
import talents from '../db/talent';

const datas = talents.map(v => {
  return {
    ...v,
    Type: v.Co1 > 20000 ? 1 : 0,
    Co8: v.Co8 ? v.Co8 : 0,
  };
});

console.log(datas[0]);

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'Zhao5162300..0',
  database: 'xxsg_db',
});

connection.connect((err: any) => {
  if (err) {
    console.log(err);
    return;
  }

  console.log('数据库连接成功');
  const insertQuery =
    'INSERT INTO config_talent (id, name, description, talent, value, type, co1, co2, co3, co4, co5, co6, co7, co8) VALUES ?';

  connection.query(
    insertQuery,
    [datas.map((data: any) => Object.values(data))],
    (err: any, result: { affectedRows: any }) => {
      if (err) {
        console.error('插入数据失败:', err);
        return;
      }

      console.log(`插入 ${result.affectedRows} 条数据.`);
    },
  );

  // 关闭连接
  connection.end();
});
